// 交互相关hooks

import { useState, useCallback } from 'react';
import {
  toggleLike as apiToggleLike,
  dislikeContent as apiDislikeContent,
  createComment as apiCreateComment,
  deleteComment as apiDeleteComment,
  getComments as apiGetComments,
  getLikeStatus as apiGetLikeStatus,
} from '../api';
import {
  LikeToggleRequest,
  DislikeRequest,
  CreateCommentRequest,
  LikeStatusRequest,
  CommentQueryParams,
  CommentListResponse,
} from '../types';
import { handleApiError } from '../utils/api';

export const useInteraction = () => {
  const [loading, setLoading] = useState(false);
  const [error, setError] = useState<string | null>(null);

  // 点赞切换
  const toggleLike = useCallback(async (data: LikeToggleRequest) => {
    setLoading(true);
    setError(null);
    
    try {
      const response = await apiToggleLike(data);
      if (response.code === 200) {
        return response.data; // 返回操作结果消息
      } else {
        throw new Error(response.message);
      }
    } catch (err) {
      const errorMessage = handleApiError(err);
      setError(errorMessage);
      throw new Error(errorMessage);
    } finally {
      setLoading(false);
    }
  }, []);

  // 点踩内容
  const dislikeContent = useCallback(async (data: DislikeRequest) => {
    setLoading(true);
    setError(null);
    
    try {
      const response = await apiDislikeContent(data);
      if (response.code === 200) {
        return response.data; // 返回操作结果消息
      } else {
        throw new Error(response.message);
      }
    } catch (err) {
      const errorMessage = handleApiError(err);
      setError(errorMessage);
      throw new Error(errorMessage);
    } finally {
      setLoading(false);
    }
  }, []);

  // 创建评论
  const createComment = useCallback(async (data: CreateCommentRequest) => {
    setLoading(true);
    setError(null);
    
    try {
      const response = await apiCreateComment(data);
      if (response.code === 200) {
        return response.data; // 返回操作结果消息
      } else {
        throw new Error(response.message);
      }
    } catch (err) {
      const errorMessage = handleApiError(err);
      setError(errorMessage);
      throw new Error(errorMessage);
    } finally {
      setLoading(false);
    }
  }, []);

  // 删除评论
  const deleteComment = useCallback(async (commentId: number) => {
    setLoading(true);
    setError(null);
    
    try {
      const response = await apiDeleteComment(commentId);
      if (response.code === 200) {
        return response.data; // 返回操作结果消息
      } else {
        throw new Error(response.message);
      }
    } catch (err) {
      const errorMessage = handleApiError(err);
      setError(errorMessage);
      throw new Error(errorMessage);
    } finally {
      setLoading(false);
    }
  }, []);

  // 获取评论列表
  const getComments = useCallback(async (params: CommentQueryParams) => {
    setLoading(true);
    setError(null);
    
    try {
      const response = await apiGetComments(params);
      if (response.code === 200) {
        return response.data;
      } else {
        throw new Error(response.message);
      }
    } catch (err) {
      const errorMessage = handleApiError(err);
      setError(errorMessage);
      throw new Error(errorMessage);
    } finally {
      setLoading(false);
    }
  }, []);

  // 查询点赞状态
  const getLikeStatus = useCallback(async (data: LikeStatusRequest) => {
    setLoading(true);
    setError(null);
    
    try {
      const response = await apiGetLikeStatus(data);
      if (response.code === 200) {
        return response.data; // 返回布尔值
      } else {
        throw new Error(response.message);
      }
    } catch (err) {
      const errorMessage = handleApiError(err);
      setError(errorMessage);
      throw new Error(errorMessage);
    } finally {
      setLoading(false);
    }
  }, []);

  return {
    loading,
    error,
    toggleLike,
    dislikeContent,
    createComment,
    deleteComment,
    getComments,
    getLikeStatus,
  };
}; 